//
//  CWaterFall.m
//  HotDisPlus
//
//  Created by yilong zhang on 12-6-13.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "CWaterFall.h"

//=========================================================================================================

@implementation CWaterDrop

@synthesize m_ImgBtn;
@synthesize m_Image;
@synthesize m_Obj;

- (id)initWithFrame:(CGRect)frame Image:(UIImage *)image Tag:(int)tag
{
    self = [super initWithFrame:frame];
    if (self)
    {
        self.tag = tag;
        
        m_Image = [[UIImageView alloc] initWithFrame:CGRectMake(5, 5, frame.size.width-5, frame.size.height-5)];
        m_Image.backgroundColor = [UIColor whiteColor];
        m_Image.image = image;
        [self addSubview:m_Image];
        [m_Image release];
        
        m_ImgBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        m_ImgBtn.alpha = 0.1;
        m_ImgBtn.frame = m_Image.frame;
        m_ImgBtn.tag = self.tag;
        [self addSubview:m_ImgBtn];
    }
    
    return self;
}

-(void)dealloc
{
    [super dealloc];
}

- (void)AddTargetForClickedEvent:(id)target Sel:(SEL)sel
{
    [m_ImgBtn addTarget:target action:sel forControlEvents:UIControlEventTouchUpInside];
}

@end

//=========================================================================================================

@implementation CWaterFall

@synthesize delegate;
@synthesize m_RootPanel;
@synthesize m_Drops;
@synthesize m_WaterDropSize;
@synthesize m_nTotal;
@synthesize m_nRows;
@synthesize m_nClos;
@synthesize m_nColCnt;

- (id)initWithFrame:(CGRect)frame ColNum:(int)colnum WaterDropSize:(CGSize)size
{
    self = [super initWithFrame:frame];
    if (self) 
    {
        m_Drops = [[NSMutableArray array] retain];
        
        m_nClos = colnum;
        m_WaterDropSize = CGSizeMake(size.width, size.height);
        
        m_RootPanel = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
        m_RootPanel.contentSize = CGSizeMake(m_RootPanel.contentSize.width, m_RootPanel.frame.size.height+1);
        m_RootPanel.delegate = self;
        m_RootPanel.pagingEnabled = YES;
        m_RootPanel.backgroundColor = [UIColor clearColor];
        [self InitWaterFall];
        [self addSubview:m_RootPanel];
        [m_RootPanel release];
    }
    
    return self;
}

- (void)dealloc
{
    [m_Drops release];
    
    [super dealloc];
}

- (void)InitWaterFall
{
    [m_Drops removeAllObjects];
    
    m_nColCnt = 0;
    m_nRows   = 0;
    m_nTotal  = 0;
    
    for (CWaterDrop *drop in [m_RootPanel subviews])
    {
        [drop removeFromSuperview];
    }

    m_RootPanel.contentSize = CGSizeMake(m_RootPanel.contentSize.width, m_RootPanel.frame.size.height+1);
    [m_RootPanel setContentOffset:CGPointMake(0, 0)];
}

- (CWaterDrop*)AddWaterDrop:(UIImage *)image
{
    if (m_nColCnt > 0)
    {
        if (0 == m_nColCnt%m_nClos)
        {
            m_nColCnt= 0;
            m_nRows += 1;
            
            if ((m_nRows+1)*m_WaterDropSize.height >= m_RootPanel.contentSize.height)
                m_RootPanel.contentSize = CGSizeMake(m_RootPanel.contentSize.width, (m_nRows+1)*m_WaterDropSize.height);
        }
    }
    
    CGRect rect = CGRectMake(m_nColCnt*m_WaterDropSize.width, m_nRows*m_WaterDropSize.height, m_WaterDropSize.width, m_WaterDropSize.height);
    CWaterDrop *waterdrop = [[CWaterDrop alloc] initWithFrame:rect Image:image Tag:m_nTotal++];
    [waterdrop AddTargetForClickedEvent:self Sel:@selector(OnWaterDropDown:)];
    [m_RootPanel addSubview:waterdrop];
    [m_Drops addObject:waterdrop];
    [waterdrop release];
    
    m_nColCnt += 1;
    
    return waterdrop;
}

- (void)OnWaterDropDown:(UIButton *)sender
{
    [delegate WaterFallSelected:self Index:sender.tag];
}

- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView
{
    CGPoint point = scrollView.contentOffset;
    
    //more
    if ((point.y+scrollView.frame.size.height) - scrollView.contentSize.height > 80 && point.y > 80)
    {
        [delegate DidWaterFallButtom:self];
    }
}

@end
